<?php


namespace app\common\service\order\coupon_order\meituan;


use api\channel\meituan\alliance\Seckill;
use app\common\service\order\coupon_order\Base;
use app\common\service\order\coupon_order\CouponOrderChannelInterface;
use app\model\Agent;
use app\model\AgentServiceConfig;
use app\model\CouponOrder;
use GaoweiSpace\MeituanPubUnion\Http\Client;
use think\Exception;


//美团分销联盟
//https://pub.meituan.com/#/api-doc

abstract class MeituanChannelBase extends Base implements CouponOrderChannelInterface
{


    /**
     * 同步订单
     * @return mixed
     */
    abstract public function syncOrder();


    /**
     * 类型 常量类型
     * @return mixed
     */
    abstract public function getServType();

    /**
     * 产品名称 可以是title
     * @return mixed
     */
    abstract public function getProductName();

    /**
     *  获取结算状态
     * @return mixed
     */
    abstract public function getAgentSettleStatus();

    /**
     * 获取状态
     * @return mixed
     */
    abstract public function getStatus();

    /**
     * 退款金额
     */
    public function getRefundAmount()
    {
        return 0;
    }
    /**
     * 获取美团的标识&解密完的
     */
    private function utmMedium()
    {

        //需要解密一下
        /**
         * @var Seckill $extend
         */
        $extend = $this->extend;
        //只要秘钥就好了
        $client = new Client($extend->apikey, '');

        return $client->decrypt($this->params['utmMedium'] ?? '');
    }

    public function getTitle()
    {
        return $this->params['orderType'] ?? '美团';
    }

    public function getSupplierOrderSn()
    {
        return $this->params['uniqueItemId'] ?? '';
    }

    /**
     * 获取第三方订单号
     */
    public function getOutOrderId()
    {
        if (!empty($this->params['orderId'])){
            return $this->params['orderId'];
        }
        return $this->getSupplierOrderSn();
    }

    //用户支付的金额
    public function getAmount()
    {
        if (empty($this->params['actualItemAmount'])) {
            throw new Exception('没有' . 'actualItemAmount' . '字段');
        }
        return yuan2fen($this->params['actualItemAmount']);
    }


    public function getOrderAllCommission()
    {
        return $this->params['balanceAmount'] ?? 0;
    }


    public function getAgentId()
    {

        $msg = '代理商不存在，过滤了';

        if (empty($this->_agent_id)) {
            throw new Exception($msg);
        };

        if (!Agent::find($this->_agent_id)) {
            $msg = '代理商不存在，过滤了';
            echo $msg . PHP_EOL;
            \think\facade\Log::info($msg);
            throw new Exception($msg);
        };

        return $this->_agent_id;

    }

    //原价
    public function getOriginAmount()
    {
        return ''; //好像没有
    }

    public function getPayAmount()
    {

        $payamount = $this->params['actualItemAmount'] ?? 0;

        return yuan2fen($payamount);
    }

    public function getPayStatus()
    {
        //https://inews.gtimg.com/newsapp_ls/0/14888486106/0.jiketuchuang.png

        return CouponOrder::C_PAY_STATUS_YES;  //都是支付过的，才有记录
    }

    public function getPayTime()
    {
        return $this->params['orderPayTime'] ?? null;
    }

    /**
     * 获取渠道标识
     */
    public function getSid()
    {
        return $this->utmMedium();
    }

    /**
     * 用户id
     */
    public function getUserId()
    {

        return $this->_user_id;

    }

    public function getStatusDesc()
    {
        return '';//不管咯
    }

    public function getRefundStatus()
    {
        //状态 ： https://inews.gtimg.com/newsapp_ls/0/14888931121/0.jiketuchuang.png

        $itemStatus = $this->params['itemStatus'] ?? -1;

        switch ($itemStatus) {
            case 0:
            case 1:
                return CouponOrder::C_REFUND_STATUS_NO;
            case 2:
                return CouponOrder::C_REFUND_STATUS_ING;
            case 3:
            case 5:
                return CouponOrder::C_REFUND_STATUS_HAVE;
            default:
                \think\facade\Log::warning('未知美团联盟状态');
        }


        return CouponOrder::C_REFUND_STATUS_NO;
    }


    /**
     * 结算时间
     */
    public function getSettledAt()
    {

        $verifyTime = $this->pay_time ?? null;

        if ($verifyTime) {
            $settleAt = $this->getSettleDate($verifyTime);
        }

        return $settleAt;

    }


    public function getType()
    {
        //子类型
        //https://inews.gtimg.com/newsapp_ls/0/14888418316/0.jiketuchuang.png
//        return $this->params["orderType"] ?? 0;
        return 0;
    }

    /**
     * 获取渠道
     */
    public function getCnid()
    {

        return $this->_channel_id;
    }

    public function getSettleType()
    {
        if ($this->_income_type == AgentServiceConfig::INCOME_TYPE_SELF){
            return  CouponOrder::C_SETTLE_TYPE_AGENT_SELF;
        }else{
            return CouponOrder::C_SETTLE_TYPE;
        }
    }


    public function getProductImage()
    {
        return $this->params['shopImage'] ?? 'https://s1.ax1x.com/2022/05/19/OH0ilR.png';
    }

    /**
     * 订单下单时间
     */
    public function getOrderCreatedAt()
    {
        return $this->pay_time ?? null;
    }


}